/*
    方便在油猴中引入代码，使用直接执行方法。
 */
// ==UserScript==
// @name         鼠标快捷键增强能力
// @namespace
// @version      0.1
// @description  快捷键 - 鼠标和键盘的快捷键
// @author       You
// @icon         http://wuweijun.fun/favicon.ico
// @icon         C:\weijun.wu\learning_code\JavaScript\favicon.ico
// @match        http://*/*
// @match        https://*/*
// ==/UserScript==

(function () {
  // 'use strict';
  // window.onload = function () { };

  // 键盘快捷键 - 处理的方法
  let exeObjectKeydown = {
      "*": (event) => {
          if (event.keyCode == 37 || event.code == 'ArrowLeft') {
              document.querySelectorAll('a').forEach((item) => {
                  // (item.innerText.trim() == '上一章' || item.title.trim() == '上一章' || item.innerText.trim() == '上一页' || item.title.trim() == '上一页') && item.click()
                  [ '上一章', '上一页'].forEach((name)=>{
                      (item.innerText.trim() == name || item.title.trim() == name) && item.click()
                  })
              })
          } else if (event.keyCode == 39 || event.code == 'ArrowRight') {
              document.querySelectorAll('a').forEach((item) => {
                  // (item.innerText.trim() == '下一章' || item.title.trim() == '下一章' || item.innerText.trim() == '下一页' || item.title.trim() == '下一页') && item.click()
                  // (item.innerText.trim().indexOf('下一章') != -1 || item.title.trim().indexOf('下一章') != -1 || item.innerText.trim().indexOf('下一页') != -1 || item.title.trim().indexOf('下一页') != -1) && item.click()
                  [ '下一页', '下一章'].forEach((name)=>{
                      (item.innerText.trim().indexOf(name) != -1 || item.title.trim().indexOf(name) != -1) && item.click()
                  })
              })
          }
      },
  }

  // 鼠标右键快捷 - 处理的方法
  let exeObjectMousedown = {
      "www.baidu.com": () => {
          let a = document.querySelectorAll("a.n");
          a[a.length - 1].click()
      },
      "www.qidian.com": () => {
          document.querySelectorAll(".lbf-pagination-next")[0].click();
          document.querySelector(".lbf-pagination-next").href == 'javascript:;' && document.querySelectorAll(".lbf-pagination-page")[4].click();
      },
      "*": () => {
          let aTip = {};
          document.querySelectorAll('a').forEach((item) => {
              let text = item.innerText.trim();
              aTip[text] = item;
          })
          let aTipArr= Object.keys(aTip);
          let target = ['下一页', '下一章', '下一话'];
          for (let index = 0; index < target.length; index++) {
              const element = target[index];
              let t = aTipArr.find((item2) => item2.includes(element));
              if (t) {
                  aTip[t].click();
                  break;
              }
          }
      },
  }

  // 鼠标滚轮 - 处理的方法
  let exclude = ["qidian", 'qq', 'qdmm', 'readnovel', 'xs8','rongshuxia','hongxiu', 'qidiantu'];
  let exeObjectRoller = {
      "www.baidu.com": () => {
          document.querySelectorAll("h3 a").forEach((item) => {
              item.click();
          })
          if(document.querySelector("#page > div > a:nth-child(12)")) document.querySelector("#page > div > a:nth-child(12)").click();
          else document.querySelector("#page > div > a.n").click();
      },
      "cn.bing.com": () => {
          console.clear();
          document.querySelectorAll(".b_algo .b_tpcn > a").forEach((item) => {
              console.log(item.href)

              // let href = item.href.split('/')[2].split('.');

              let href = item.querySelector('.tpmeta').innerText.split('/')[2].split('.');

              if(exclude.indexOf(href[href.length -2]) == -1){
                  item.click();
              }
          })
          document.querySelector(".sb_pagN").click();
      },
      "www.bing.com": () => {
          console.clear();
          document.querySelectorAll(".b_algo h2 a").forEach((item) => {
              console.log(item.href)

              let href = item.href.split('/')[2].split('.');

              if(exclude.indexOf(href[href.length -2]) == -1){
                  item.target = "_blank"; item.click();
              }
          })
          document.querySelector(".sb_pagN").click();
      },
      "*": () => {
          let aTip = {};
          document.querySelectorAll('a').forEach((item) => {
              let text = item.innerText.trim();
              aTip[text] = item;
          })
          let target = ['下一页', '下一章', '下一话','下一章 >','下一页 >'];
          for (let index = 0; index < target.length; index++) {
              const element = target[index];
              if (aTip[element]) {
                  aTip[element].click();
                  break;
              }
          }
      },
  }

  // 键盘快捷键
  document.addEventListener('keydown', keydown);
  let flag1;
  function keydown(ev) {
      var event = ev || event
      Object.keys(exeObjectKeydown).forEach((item, index) => {
          location.hostname == item && (exeObjectKeydown[item](event), flag1 = true);
          item == "*" && exeObjectKeydown[item](event);
      })
      flag1 && document.removeEventListener('keydown', keydown);
  }

  // 鼠标快捷
  document.addEventListener('mousedown', onMouseDown);
  function onMouseDown(event) {
      if (event.button == 0) {  // 鼠标左键
      } else if (event.button == 2) { // 鼠标右键
          setTime();
      } else if (event.button == 1) {  //鼠标滚轮
          Roller()
      }
  }

  /* 点击两次鼠标右键 */
  let flag3 = false;   // 用于表示是否点击两次鼠标右键
  let tt, ff;
  function setTime() {
      if (ff) return;
      if (flag3) {
          ff || Object.keys(exeObjectMousedown).forEach((item, index) => {
              location.hostname == item && ((window.alert = () => { }), exeObjectMousedown[item](), tt = true);
              item == "*" && exeObjectMousedown[item]();
          });
          tt || (ff = true);
      }
      flag3 = true;
      setTimeout(() => {
          flag3 = false;
      }, 3000);
  }

  /* 点击两次鼠标滚轮 */
  let flag2 = false;   // 用于表示是否点击两次鼠标滚轮
  function Roller() {
      if (flag2) {
          Object.keys(exeObjectRoller).forEach((item, index) => {
              location.hostname == item && ((window.alert = () => { }), exeObjectRoller[item]());
              item == "*" && exeObjectMousedown[item]();
          });
      }
      flag2 = true;
      setTimeout(() => {
          flag2 = false;
      }, 3000);
  }


})();